package com.performax.service;


import java.util.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.performax.database.Vo.*;
import com.performax.database.Dao.*;

 
@Controller
@RequestMapping("/app/api/event")
public class OheGameEventController {
	
	private static final Logger logger = LoggerFactory.getLogger(OheGameEventController.class);
	
	@Resource(name = "oh2gameEventDao")
	private Oh2GameEventDao oh2gameEventDao;
	
	@Resource(name = "oh2gameGlobalDao")
	private	Oh2GameGlobalDao oh2gameGlobalDao;
	
	@RequestMapping(value="/eventlist")
	@ResponseBody
	public Object eventlist( @RequestParam(value="authKey", required=true) 				String authKey
							,@RequestParam(value="mediaCd", defaultValue="mcdoheapp")	String mediaCd
							,@RequestParam(value="userId", required=true) 				String userId
							,@RequestParam(value="gid", defaultValue="XXXXXXXXXX") 		String gid
							,@RequestParam(value="reqType", required=true) 				String reqType
							,@RequestParam(value="reqPage", defaultValue="1") 			int reqPage
							,@RequestParam(value="sortType", defaultValue="new") 		String sortType
							,@RequestParam(value="search", required=false) 				String search
							,@RequestParam(value="listPerPage", defaultValue="30") 		int listPerPage
							,HttpServletRequest req )
	{
		
		int start	=	(reqPage * listPerPage)-listPerPage; 
		
		//parameter set for query
		HashMap<String,Object> param	=	new	HashMap<String, Object> ();
		param.put("reqType",		reqType);
		param.put("search", 		search);
		param.put("sortType", 		sortType);
		param.put("listPerPage",	listPerPage); 
		param.put("start", 			start);
		
		//get event list 
		List<EventInfoVo> eventList	=	oh2gameEventDao.getEventList(param);
		
		//get list count
		int		listCnt	=	oh2gameEventDao.getEventListCnt(param);
		
		String	lastPage	=	"Y";
		
		if(listCnt	>	(reqPage*listPerPage))	{
			lastPage	=	"N";
			
		}

		//set result
		Hashtable<String, Object> result	=	new	Hashtable<String, Object>();
		result.put("resType",	"eventlist");
		result.put("result",	"1000");
		result.put("listCnt",	eventList.size());
		result.put("pageNo",	reqPage);
		result.put("lastPage",	lastPage);
		result.put("eventInfo",eventList);
		result.put("resultMessage", param);
		
		logger.debug("/eventlist", result);
		
		return result;
	}
	
	@RequestMapping(value="/eventdetail")
	@ResponseBody
	public Object eventdetail( @RequestParam(value="authKey", required=true) 				String authKey
								,@RequestParam(value="mediaCd", defaultValue="mcdoheapp")	String mediaCd
								,@RequestParam(value="userId", required=true) 				String userId
								,@RequestParam(value="gid", defaultValue="XXXXXXXXXX") 		String gid
								,@RequestParam(value="eventId", required=true) 				int eventId
								,HttpServletRequest req )
	{
		Hashtable<String, Object> param	=	new	Hashtable<String, Object>();
		
		param.put("authKey", authKey);
		param.put("mediaCd", mediaCd);
		param.put("userId", userId);
		param.put("gid", gid);
		param.put("eventId", eventId);
		
		
		Hashtable<String, Object> result	=	new	Hashtable<String, Object>();

		result.put("resType","eventdetail");
		result.put("eventId", eventId);
		result.put("result", "1000");
		
		EventDetailVo	eventInfo	=	oh2gameEventDao.getEventDetail(eventId);
		PreregVo		preregInfo	=	oh2gameEventDao.getPreregInfo(eventId, userId);
		String regType		=	"N";
		String couponCode	=	"";
		if(preregInfo	!=	null){
			regType	=	"Y";
			if(preregInfo.getCouponCode()	!=	null)	{
				couponCode	=	preregInfo.getCouponCode();
			}
		}
		result.put("boardCnt",		eventInfo.getBoardCnt());
		result.put("regCnt",		eventInfo.getRegCnt());
		result.put("friendCount",	eventInfo.getFriendCnt());
		result.put("regType",		regType);
		
		result.put("couponCode",	couponCode);
		result.put("downloadUrl",	eventInfo.getDownloadUrl());
		
		
		EventBaseInfoVo		eventBaseInfo	=	new	EventBaseInfoVo();
		eventBaseInfo.setOpenDate(eventInfo.getOpenDate());
		eventBaseInfo.setReward(eventInfo.getReward());
		eventBaseInfo.setThumbimgUrl(eventInfo.getThumbImgUrl());
		eventBaseInfo.setTitle(eventInfo.getTitle());
		
		EventIntroInfoVo	eventIntroInfo	=	new	EventIntroInfoVo();
		eventIntroInfo.setIntroHtml(eventInfo.getIntroHtml());
		//eventIntroInfo.setIntroVideoUrl("http://www.youtube.com/watch?v=cmKuGxb23z0");
		
		RewardDtlVo			rewardDtlInfo	=	new	RewardDtlVo();
		rewardDtlInfo.setCodeMethod(eventInfo.getCodeMethod());
		rewardDtlInfo.setCodeValidDt(eventInfo.getCodeValidDt());
		rewardDtlInfo.setRewardHtml(eventInfo.getRewardHtml());
		
		result.put("eventBaseInfo", eventBaseInfo);
		result.put("eventIntroInfo", eventIntroInfo);
		result.put("rewardDtlInfo", rewardDtlInfo);
		
		
				
		result.put("resultMessage", param);
		
		logger.debug("/eventdetail", result);
		return result;
	}
	
	@RequestMapping(value="/replylist")
	@ResponseBody
	public Object replylist( @RequestParam(value="authKey", required=true) 				String authKey
							,@RequestParam(value="mediaCd", defaultValue="mcdoheapp")	String mediaCd
							,@RequestParam(value="userId", required=true) 				String userId
							,@RequestParam(value="gid", defaultValue="XXXXXXXXXX") 		String gid
							,@RequestParam(value="eventId", required=true) 				String eventId
							,@RequestParam(value="reqPage", defaultValue="1") 			int reqPage
							,@RequestParam(value="search", required=false) 				String search
							,@RequestParam(value="sortType", defaultValue="") 			String sortType
							,@RequestParam(value="listPerPage", defaultValue="30") 		int listPerPage
							,HttpServletRequest req )
	{
		
		int start	=	(reqPage * listPerPage)-listPerPage; 
		
		HashMap<String, Object> param	=	new	HashMap<String, Object>();
		
		param.put("authKey", authKey);
		param.put("mediaCd", mediaCd);
		param.put("userId", userId);
		param.put("gid", gid);
		param.put("eventId", eventId);
		//param.put("reqPage", Integer.valueOf(reqPage));
		param.put("search", search);
		param.put("sortType", sortType);
		param.put("listPerPage", listPerPage);
		param.put("start", start);
		
		List<ReplyInfoVo> 	replyList	=	oh2gameEventDao.getEventReplyList(param);
		List<Integer>		reportList	=	oh2gameEventDao.getReplyReportType(userId);
		
		for(int i = 0 ; i < replyList.size() ; i++){
			ReplyInfoVo replyInfo	=	replyList.get(i);
			if(reportList.contains(replyInfo.getReplyId())){
				replyInfo.setReportType("Y");
			}
		}
		
		//get list count
		int		listCnt	=	oh2gameEventDao.getEventReplyListCnt(param);

		String	lastPage	=	"Y";
		
		if(listCnt	>	(reqPage*listPerPage))	{
			lastPage	=	"N";
		}
		
		
		Hashtable<String, Object> result	=	new	Hashtable<String, Object>();
		result.put("resType",	"replylist");
		result.put("eventId",	eventId);
		result.put("result",	"1000");
		result.put("listCnt",	replyList.size());
		result.put("pageNo",	reqPage);
		result.put("lastPage",	lastPage);
		result.put("replyInfo",replyList);
		
		result.put("resultMessage", param);
		
		logger.debug("/replylist", result);
		
		return result;
	}
	
	@RequestMapping(value="/replyreg")
	@ResponseBody
	public Object replyreg(  @RequestParam(value="authKey", required=true) 				String authKey
							,@RequestParam(value="mediaCd", defaultValue="mcdoheapp")	String mediaCd
							,@RequestParam(value="userId", required=true) 				String userId
							,@RequestParam(value="gid", defaultValue="XXXXXXXXXX") 		String gid
							,@RequestParam(value="eventId", required=true) 				int eventId
							,@RequestParam(value="upperId", defaultValue="0") 			int upperId
							,@RequestParam(value="category", required=true) 			String category
							,@RequestParam(value="contents", required=true) 			String contents
							,HttpServletRequest req )
	{
		
		HashMap<String, Object> param	=	new	HashMap<String, Object>();
		
		param.put("authKey", authKey);
		param.put("mediaCd", mediaCd);
		param.put("userId", userId);
		param.put("gid", gid);
		param.put("eventId", eventId);
		param.put("upperId", upperId);
		param.put("category", category);
		param.put("contents", contents);
		
		Hashtable<String, Object> result	=	new	Hashtable<String, Object>();
				
		int	replyCnt	=	oh2gameEventDao.regEventReply(param, upperId, eventId);
		
		if(upperId	>	0){
			HashMap<String, Object> pushParam	=	new	HashMap<String, Object> ();
			
			pushParam.put("targetEventType", "EVENT_REPLY");
			pushParam.put("targetEventID", upperId);
			
			oh2gameGlobalDao.regPushTarget(pushParam);
		}
		
		
		result.put("resType","replyreg");
		result.put("eventId",eventId);
		result.put("result","1000");
		result.put("replyCnt",replyCnt);

		result.put("resultMessage", param);
		
		logger.debug("/replyreg", result);
		return result;
	}
	
	
	@RequestMapping(value="/replydelete")
	@ResponseBody
	public Object replydelete( 	@RequestParam(value="authKey", required=true) 				String authKey
								,@RequestParam(value="mediaCd", defaultValue="mcdoheapp")	String mediaCd
								,@RequestParam(value="userId", required=true) 				String userId
								,@RequestParam(value="gid", defaultValue="XXXXXXXXXX") 		String gid
								,@RequestParam(value="eventId", required=true) 				int eventId
								,@RequestParam(value="replyId", required=true) 				int replyId
								,HttpServletRequest req )
	{
		
		Hashtable<String, Object> result	=	new	Hashtable<String, Object>();
		result.put("resType","replydelete");
		result.put("eventId",eventId);
		
		int replyCnt	=	oh2gameEventDao.deleteEventReply(eventId, replyId);
		
		result.put("result","1000");
		result.put("replyCnt",replyCnt);
		
		Hashtable<String, Object> param	=	new	Hashtable<String, Object>();
		
		param.put("authKey", authKey);
		param.put("mediaCd", mediaCd);
		param.put("userId", userId);
		param.put("gid", gid);
		param.put("eventId", eventId);
		param.put("replyId", replyId);
				
		result.put("resultMessage", param);
		
		logger.debug("/replydelete", result);
		return result;
	}

	@RequestMapping(value="/friendlist")
	@ResponseBody
	public Object friendlist(@RequestParam(value="authKey", required=true) 				String authKey
							,@RequestParam(value="mediaCd", defaultValue="mcdoheapp")	String mediaCd
							,@RequestParam(value="userId", required=true) 				String userId
							,@RequestParam(value="gid", defaultValue="XXXXXXXXXX") 		String gid
							,@RequestParam(value="eventId", required=true) 				int eventId
							,HttpServletRequest req )
	{
		
		Hashtable<String, Object> param	=	new	Hashtable<String, Object>();
		
		param.put("authKey", authKey);
		param.put("mediaCd", mediaCd);
		param.put("userId", userId);
		param.put("gid", gid);
		param.put("eventId", eventId);
		
		
		
		Hashtable<String, Object> result	=	new	Hashtable<String, Object>();
		result.put("resType","friendlist");
		result.put("eventId",eventId);
		
		List<FriendInfoVo> friendList	=	oh2gameEventDao.getFriendList(eventId);
		String status	=	"N";
		
		
		if(friendList	==	null)	{
			result.put("result","1000");
			result.put("status",status);
			result.put("listCount", 0);
			result.put("resultMessage", "result not found");
			
			return result;
		}
		
		
		for(int i = 0 ; i < friendList.size() ; i++){
			FriendInfoVo	friendInfo	=	friendList.get(i);
			if(friendInfo.getId().equals(userId)){
				friendList.remove(i);
				status	=	"Y";
				break;
			}
		}
		
		result.put("result","1000");
		result.put("status",status);
		result.put("listCount", friendList.size());
		result.put("friendInfo", friendList);
				
		result.put("resultMessage", param);
		return result;
	}

	@RequestMapping(value="/friendreg")
	@ResponseBody
	public Object friendreg(@RequestParam(value="authKey", required=true) 				String authKey
							,@RequestParam(value="mediaCd", defaultValue="mcdoheapp")	String mediaCd
							,@RequestParam(value="userId", required=true) 				String userId
							,@RequestParam(value="gid", defaultValue="XXXXXXXXXX") 		String gid
							,@RequestParam(value="eventId", required=true) 				int eventId
							,@RequestParam(value="regType", required=true) 				String regType
							,HttpServletRequest req )
	{
		Hashtable<String, Object> param	=	new	Hashtable<String, Object>();
		
		param.put("authKey", authKey);
		param.put("mediaCd", mediaCd);
		param.put("userId", userId);
		param.put("gid", gid);
		param.put("eventId", eventId);
		param.put("regType", regType);
		
		int	friendCnt	=	oh2gameEventDao.regFriend(eventId, userId, regType);
		
		Hashtable<String, Object> result	=	new	Hashtable<String, Object>();
		result.put("resType","friendreg");
		result.put("eventId",eventId);
		result.put("result","1000");
		result.put("status", regType);
		result.put("friendCnt", friendCnt);
				
		result.put("resultMessage", param);
		return result;
	}

	@RequestMapping(value="/prereg")
	@ResponseBody
	public Object prereg(@RequestParam(value="authKey", required=true) 				String authKey
						,@RequestParam(value="mediaCd", defaultValue="mcdoheapp")	String mediaCd
						,@RequestParam(value="userId", required=true) 				String userId
						,@RequestParam(value="gid", defaultValue="XXXXXXXXXX") 		String gid
						,@RequestParam(value="eventId", required=true) 				int eventId
						,HttpServletRequest req )
	{
		
		Hashtable<String, Object> result	=	new	Hashtable<String, Object>();
		result.put("resType","prereg");
		result.put("eventId",eventId);
		
		HashMap<String, Object> param	=	new	HashMap<String, Object>();
		
		param.put("authKey", authKey);
		param.put("mediaCd", mediaCd);
		param.put("userId", userId);
		param.put("gid", gid);
		param.put("eventId", eventId);
		
		
		Hashtable<String, Object> rtn	=	oh2gameEventDao.prereg(eventId, userId);
		
		result.put("result",		"1000");
		result.put("couponCode", 	rtn.get("couponCode"));
		result.put("regCnt", 		rtn.get("regCnt"));
				
		result.put("resultMessage", param);
		return result;
	}

	@RequestMapping(value="/replyreport")
	@ResponseBody
	public Object replyreport(@RequestParam(value="authKey", required=true) 			String authKey
							,@RequestParam(value="mediaCd", defaultValue="mcdoheapp")	String mediaCd
							,@RequestParam(value="userId", required=true) 				String userId
							,@RequestParam(value="gid", defaultValue="XXXXXXXXXX") 		String gid
							,@RequestParam(value="eventId", required=true) 				int eventId
							,@RequestParam(value="replyId", required=true) 				int replyId
							,@RequestParam(value="reasonCheck", required=true) 			String reasonCheck
							,@RequestParam(value="reasonCmt", required=false) 			String reasonCmt
							,HttpServletRequest req )
	{
		Hashtable<String, Object> result	=	new	Hashtable<String, Object>();
		result.put("resType","replyreport");
		result.put("eventId",eventId);

		
		StringBuffer	reason	=	new	StringBuffer();
		reason.append(reasonCheck);
		if(reasonCmt	!=	null)	reason.append("||").append(reasonCmt);
		
		HashMap<String, Object> param	=	new	HashMap<String, Object>();
		
		param.put("authKey", 		authKey);
		param.put("mediaCd", 		mediaCd);
		param.put("userId", 		userId);
		param.put("gid", 			gid);
		param.put("eventId", 		eventId);
		param.put("replyId", 		replyId);
		param.put("reasonCheck",	reasonCheck);
		param.put("reasonCmt", 		reasonCmt);
		param.put("reason", 		reason.toString());
		
		int reportCount	=	oh2gameEventDao.reportEventReply(param, replyId);
		
		/* push */
		HashMap<String, Object> pushParam	=	new	HashMap<String, Object> ();
		
		pushParam.put("targetEventType", "EVENT_REPLY_REPORT");
		pushParam.put("targetEventID", replyId);

		oh2gameGlobalDao.regPushTarget(pushParam);
		/* push */
		
		result.put("replyId",replyId);
		result.put("result","1000");
		result.put("repCnt", reportCount);	//신고수 처리 필
		
				
		result.put("resultMessage", param);
		return result;
	}

}

